<?php

/**
 * 号码归属地控制器
 *
 * @author Alen
 * @since 1.0 2015-1-5 by sutroon
 */
class NumberRegionalAction extends BaseAction {

    // 获取归属地
    public function get_regional($tel = '') {
        // 3,4,5,7
        $tel = trim($tel);
        if (!$tel) {
            die('');
        }
        $len = strlen($tel);
        if ($len < 8) { // 不是固话也不是手机
            die('');
        }
        if ($len > 10) { // 手机号过滤前缀0
            $tel = ltrim($tel, '0');
            if (!$tel) {
                die('');
            }
            $len = strlen($tel);
        }
        $arrISPStart = array(
            'yidong' => "134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188",
            'liantong' => "130,131,132,145,155,156,185,186",
            'dianxin' => "133,153,180,181,189",
        );
        $start = substr($tel, 0, 3);
        $isp = '';
        foreach ($arrISPStart as $key => $item) {
            if (false !== strpos(",$item,", ",$start,")) {
                $isp = $key;
                break;
            }
        }
        if ($isp) {
            $starts[] = substr($tel, 0, 7); // 手机
        } else {
            $starts[] = substr($tel, 0, 5); // 固话
            $starts[] = substr($tel, 0, 4);
            $starts[] = substr($tel, 0, 3);
        }
        foreach ($starts as $start) {
            $region = M('tcti_number_start', '', 'DB_CONFIG_CTI')->where("numberPart= '$start'")->getField('areaName');
            if ($region) {
                echo $region;
                break;
            }
        }
        echo '';
    }

    // 获取开始号码 如：areaname=厦门市
    public function number_list_json() {
        $areaname = I('areaname');
        $phoneGear = I('phonegear');
        $isp = I('isp');
        if (!$areaname)
            return '';
        if (!$phoneGear) {
            $phoneGear = "手机";
        }
        $sql = "areaName like '%$areaname%'";
        $fields = "numberPart as id, concat(replace(areaName,'(*)',''),' >> ',numberPart) as name";
        switch ($phoneGear) {
            case "手机":
                $sql .= "  and length(numberPart)>5";
                break;
            default:
                $sql .= " and length(numberPart)<5";
                $fields = "numberPart as id, concat(cast(phoneLength as varchar(6)), '位 >> 固话位数：',numberPart ) as name";
                break;
        }
// isp 移动、联通、电信
        $yidong = "134,135,136,137,138,139,147,150,151,152,157,158,159,182,183,184,187,188";
        $liantong = "130,131,132,145,155,156,185,186";
        $dianxin = "133,153,180,181,189";
        switch ($isp) {
            case "移动":
                $sql .= sprintf(" and left(numberPart,3) in ('%s') ", implode("','", explode(',', $yidong)));
                break;
            case "联通":
                $sql .= sprintf(" and left(numberPart,3) in ('%s') ", implode("','", explode(',', $liantong)));
                break;
            case "电信":
                $sql .= sprintf(" and left(numberPart,3) in ('%s') ", implode("','", explode(',', $dianxin)));
                break;
        }
        $sql = "select $fields from tcti_number_start where $sql order by numberPart";
        $M = M();
        // $list = $M->cache(true, 36000)->query($sql);
        $list = $M->query($sql);
        if ($list === false) {
            die('');
        }
        echo json_encode($list);
    }

}
